package com.example.mongologistics.controller;

import com.example.mongologistics.pojo.Logistics;
import com.example.mongologistics.pojo.Order;
import com.example.mongologistics.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Api(value = "订单controller",tags = "订单接口")
@RestController
@Slf4j
public class OrderController {
    @Autowired
    OrderService orderService;

    @ApiOperation("添加订单接口")
    @PostMapping("addorder")
    public String addorder(@ApiParam("订单") @RequestParam(value = "order",required = true) Order order) {
        order.setStatus("发货中");
        order.setOrderTime(new Date());
        order.setShipTime(new Date());
        orderService.addOrder(order);
        return "添加成功";
    }

    @ApiOperation("更新订单物流接口")
    @PostMapping("updateorder")
    public String updateorder(@ApiParam("物流对象") @RequestParam(value = "logistics",required = true)Logistics logistics) {
        logistics.setOperationTime(new Date());
        orderService.addLogisticsAndUpdateStatus(logistics);
        return "更新物流成功";
    }

    @ApiOperation("获取订单")
    @GetMapping("getorderbyid")
    public Order getOrderById(@ApiParam("订单id") @RequestParam(value = "id",required = true)int id) {
        Order order = orderService.getOrderById(id);
        return order;
    }

    @ApiOperation("删除订单")
    @GetMapping("deletebyid")
    public boolean delelteOrderById(@ApiParam("订单id") @RequestParam(value = "id",required = true)int id) {
        boolean ret = orderService.deleteOrderById(id);
        return ret;
    }

    @ApiOperation("获取所有订单")
    @GetMapping("getallorders")
    public Map<String, Object> getAllOrders(@ApiParam("第几页，默认1") @RequestParam(value = "page",required = true,defaultValue = "1") int page,
                                            @ApiParam("每页数量，默认10") @RequestParam(value = "limit",required = true,defaultValue = "10") int limit) {
        Map<String,Object> map=new HashMap<>();
        List<Order> orderList = orderService.getAllOrders();
        map.put("code","0");
        if (orderList.size() > 0) {
            map.put("count",orderList.size());
            map.put("data",orderList);
        }else{
            map.put("count", 0);
            map.put("data", null);

        }
        return map;
    }
}
